package com.eascs.web.p2p.loginAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.common.entity.AdminUserEntity;
import com.eascs.p2p.common.service.IAdminUserService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.captcha.controller.CaptchaBuilder;

@RestController
@RequestMapping("/adminRest")
public class LoginRestController extends BaseRestController {
	private final Logger logger = LoggerFactory.getLogger(LoginRestController.class);

	@Reference
	IAdminUserService adminUserService;


	@RequestMapping(value = "/login")
	public BaseRestResult<String> login(HttpServletRequest request, HttpServletResponse response, String account, String password,String code) {
		String yanzCode=CaptchaBuilder.SESSION_CAPTCHA.getValue();
		BaseRestResult<String> result = null;
		if (account.isEmpty()) {
			return result = BaseRestResult.error("用户名不能为空");
		}
		
		if (password.isEmpty()) {
			return result = BaseRestResult.error("密码不能为空");
		}
		if(!code.equalsIgnoreCase(yanzCode)){
			
			return result = BaseRestResult.error("验证码不正确");
		}
		
        if(code.isEmpty()){
			
			return result = BaseRestResult.error("验证码不能为空");
		}
		
		ModelResult<AdminUserEntity> modelResult = adminUserService.queryAdminUserByLogin(account, password);
		if (modelResult.isSuccess()) {
			AdminUserEntity admin = modelResult.getModel();
			if (admin != null) {
				if(admin.getStatus()==1){
					return result = BaseRestResult.error("您的账号已经被禁用了,无法登陆");
				}
				HttpSession session = request.getSession();
				session.removeAttribute("adminUser");
				session.setAttribute("adminUser", admin);
				return BaseRestResult.redirect("/bid/index");
			
			}
		}
		return result = BaseRestResult.warn("用户名或者密码不正确");
	}
	
	/**
	 * @Title: goOut
	 * @Description: 退出
	 * @author 张庆奇
	 * @param request
	 * @return
	 * @return BaseRestResult<String>
	 * @throws
	 * @date 2016年7月19日 上午10:52:25
	 */
	@RequestMapping(value = "/goOut")
	public BaseRestResult<String> goOut(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.removeAttribute("adminUser");
		return BaseRestResult.redirect("/admin/index");
	}
}
